# Virtual Router Simulator 
#
# Copyright (C) 2009-2010 VRSim Project
# Author: Choonho Son <choonho@kt.com>
# URL: <http://vrsim.googlecode.com>
# For license information, see LICENSE.TXT
#

import VRSim.Globals as globals
import sys,  time,  os
from optparse import OptionParser
from PyQt4 import QtCore
from PyQt4.QtGui import QApplication

from VRSim.Workspace import Workspace
# for logging
import logging
import logging.handlers

from VRSim.Ui.Login import Login
from VRSim.Ui.Topology import Topology
from VRSim.Globals.Symbols import SYMBOLS,  SYMBOL_TYPES
from VRSim.Utils import Singleton
from VRSim.Utils import debug


class Application(QApplication,  Singleton):
    
    def __init__(self,  hypervisors):
        QApplication.__init__(self,  sys.argv)
        
        # init default hypervisors
        self.default_hypervisors = hypervisors
        
        self.userid = None
        self.passwd = None
        self.authenticated = False
        self.__login = None
        self.__mainWindow = None
        self.__workspace = None
        self.__scene = None
        self.__topology = None
        self.lang = None
        self.debug = False
        
        globals.GApp = self
        
        # Logging facilities
        LOGGING = {'debug': logging.DEBUG,
                        'info': logging.INFO,
                        'warning': logging.WARNING,
                        'error': logging.ERROR,
                        'critical': logging.CRITICAL}
        self.my_logger = logging.getLogger("VRSim")
        self.my_logger.setLevel(logging.DEBUG)
        # create console handler and set level to debug
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)

        formatter = logging.Formatter("%(asctime)s - - %(levelname)s - %(message)s")
        ch.setFormatter(formatter)
        self.my_logger.addHandler(ch)
        
        # OptionParser
        parser = OptionParser()
        parser.add_option("-L", "--language", dest="language",
                  help="choose your language\n {English | Korean}", metavar="LANGUGAE")
        parser.add_option("-D", "--debug",
                  action="store_true", dest="debug", default=False,
                  help="Debug program")
                          
        (options, args) = parser.parse_args()
        
        if options.language:
            self.lang = options.language
        if options.debug:
            self.debug = options.debug
            globals.debugLevel = 2
    
    def __setLoginWindow(self,  lw):
        self.__login = lw
    
    def __getLoginWindow(self):
        return self.__login
        
    login = property(__getLoginWindow,  __setLoginWindow,  doc="Login Window")
    
    def __setMainWindow(self,  mw):
        self.__mainWindow = mw
    
    def __getMainWindow(self):
        return self.__mainWindow
        
    mainWindow = property(__getMainWindow,  __setMainWindow,  doc='MainWindow Instance')
    
    def __setWorkspace(self,  wkspc):
        self.__workspace = wkspc
    
    def __getWorkspace(self):
        return self.__workspace
        
    workspace = property(__getWorkspace, __setWorkspace,   doc = 'Workspace Instance')
    
    def __setScene(self,  scene):
        self.__scene = scene
        
    def __getScene(self):
        return self.__scene
   
    scene = property(__getScene,  __setScene,  doc='Scene Instance')
    def __setTopology(self,  topology):
        self.__topology = topology
        
    def __getTopology(self):
        return self.__topology
        
    topology = property(__getTopology,  __setTopology,  doc='Topology Instance')
    
    # initialize hypervisors information
    def initHypervisor(self):
        for res in self.default_hypervisors:
            # make initial one hypervisor per resource 
            temp_res = ["", "", "", res]
            self.topology.hypervisors.append(temp_res)
        # TEST
        print self.topology.hypervisors
         
    def run(self):
        self.__login = Login()
        self.__workspace = Workspace()
        self.__mainWindow = self.__workspace
        self.__scene = self.__mainWindow.TopologyDesigner
        
        #self.mainWindow.show()
        # update VMAPI
        #self.topology.hypervisors = self.default_hypervisors
        self.login.show()
        
        
        retcode = QApplication.exec_()
        
        sys.exit(retcode)     
